<?php
/**
 * Created by PhpStorm.
 * User: hey
 * Date: 2019-05-08
 * Time: 14:02
 */

namespace app\firm\controller;

use think\Controller;
use think\Config;
use app\firm\model\MoneyCom;
use app\firm\model\ProjectUser;
use app\firm\controller\Common;
use expand\BestSignDemo;
use think\Request;
use redis\Redis;

class Company extends Common
{
    private $mcModel;
    private $puModel;
    const  UPLOAD_SRC = '/uploads/projectuser/';

    public function _initialize()
    {
        parent::_initialize();
        $this->mcModel = new MoneyCom();
        $this->puModel = new ProjectUser();
    }

    /**
     *企业端公司资料页面
     * @author hey_heyang(635870453@qq.com)
     */
    public function companyInfo() {
        $user_info = request()->firm_info;
        $puModel = new ProjectUser();
        if(!empty($user_info)){
            $user_info = $puModel->getUserInfo(array('id'=>$user_info['id']));
        }
        $business_license=self::UPLOAD_SRC.$user_info['id'].'/'.$user_info['business_license'];
        $legal_person_id_front=self::UPLOAD_SRC.$user_info['id'].'/'.$user_info['legal_person_id_front'];
        $legal_person_id_back=self::UPLOAD_SRC.$user_info['id'].'/'.$user_info['legal_person_id_back'];
        $company_count=self::UPLOAD_SRC.$user_info['id'].'/'.$user_info['company_count'];

        switch ($user_info['status']) {
            case 0:
                $msg='待提交';
                break;
            case 1:
                $msg='平台审核中';
                break;
            case 2:
                $msg='通联审核中';
                break;
            case 3:
                $msg='平台拒绝';
                $data['msg'] = $user_info['systerm_msg'];
                break;
            case 4:
                $msg='通联拒绝';
                $data['msg'] = $user_info['tonglian_msg'];
                break;
            case 5:
                $msg='审核通过';
                break;
        }
        $data['company_name']=$user_info['company_name'];
        $data['company_intro']=$user_info['company_intro'];
        $data['legal_person']=$user_info['legal_person'];
        $data['base_count']=$user_info['base_count'];
        $data['base_bank_name']=$user_info['base_bank_name'];
        $data['company_count']=$user_info['company_count'];
        $data['mobile']=$user_info['mobile'];
        $data['legal_person_id_card'] = $user_info['legal_person_id_card'];
        $data['status'] = $user_info['status'];
        $data['business_license']=$business_license;
        $data['legal_person_id_front']=$legal_person_id_front;
        $data['legal_person_id_back']=$legal_person_id_back;
        $data['company_count']=$company_count;
        $data['origin_business_license']=$user_info['business_license'];
        $data['origin_legal_person_id_front']=$user_info['legal_person_id_front'];
        $data['origin_legal_person_id_back']=$user_info['legal_person_id_back'];
        $data['origin_company_count']=$user_info['company_count'];
        $data['safemobile'] = $user_info['safemobile'];
        $data['unicredit'] = $user_info['unicredit'];
        $data['bankName'] =$user_info['bankName'];
        $data['unionBank'] =$user_info['unionBank'];
        $data['province'] =$user_info['province'];
        $data['city'] =$user_info['city'];
        ajaxReturn($msg,1,'',$data);
    }

    /**
     *企业端公司资料提交
     * @author hey_heyang(635870453@qq.com)
     */
    public function companyInfoSubmit(){
        if(request()->isPost()){
            $puModel = new ProjectUser();
            $redis = Redis::instance();
            $user_info = request()->firm_info;
            $data = input('post.');
            $update_data['company_name']=trim($data['company_name']);
            $update_data['company_intro']=trim($data['company_intro']);
            $update_data['business_license']=trim($data['business_license']);
            $update_data['legal_person']=trim($data['legal_person']);
            $update_data['legal_person_id_front']=trim($data['legal_person_id_front']);
            $update_data['legal_person_id_back']=trim($data['legal_person_id_back']);
            $update_data['legal_person_id_card']=strtoupper(trim($data['legal_person_id_card']));
            $update_data['base_count']=trim($data['base_count']);
            $update_data['base_bank_name']=trim($data['base_bank_name']);
            $update_data['company_count']=trim($data['company_count']);
            $update_data['status'] =1;
            $update_data['unicredit'] =trim($data['unicredit']);
            $update_data['bankName'] =trim($data['bankName']);
            $update_data['unionBank'] =trim($data['unionBank']);
            $update_data['province'] =trim($data['province']);
            $update_data['city'] =trim($data['city']);
            $res = $puModel->where(array('id'=>$user_info['id']))->update($update_data);
            file_put_contents('companyInfo2.0.txt',str_replace("\\", "", json_encode($data,JSON_UNESCAPED_UNICODE)).$res.PHP_EOL,FILE_APPEND);
            if($res){
                if(empty($user_info['account'])){
                    //同时注册上上签的企业会员申请证书
                    $Best_data= new BestSignDemo();
                    $mobile=$user_info['mobile'];
                    $name=trim($data['company_name']);
                    $userType='2';
                    $credential['regCode']=trim($data['unicredit']);
                    $credential['orgCode']=trim($data['unicredit']);
                    $credential['taxCode']=trim($data['unicredit']);
                    $credential['legalPerson']=trim($data['legal_person']);
                    $credential['legalPersonIdentity']=strtoupper(trim($data['legal_person_id_card']));
                    $credential['legalPersonIdentityType']='0';
                    $credential['legalPersonMobile']=$user_info['mobile'];
                    $credential['contactMobile']=$user_info['mobile'];
                    $applyCert='1';
                    $account = 'ssq'.$user_info['mobile'];  //上上签唯一标识
                    $res=$Best_data->regUser($account,$mobile, $name, $userType, $credential, $applyCert);
                    $res=json_encode($res);
                    $res=json_decode($res,true);
                    if($res['errno']==0){
                        $puModel->where(array('id'=>$user_info['id']))->update(['account'=>$account]);
                    }else{
                        $error['account'] = $account;
                        $error['mobile'] = $user_info['mobile'];
                        $error['error'] = '企业端上上签时有错误。错误信息为:'.$res['errmsg'];
                        file_put_contents(date("Y-m-d H", time()) . 'batch_order_list.txt', json_encode($error,JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
                    }
                }

//                $user_info['company_name']=$data['company_name'];
//                $user_info['company_intro']=$data['company_intro'];
//                $user_info['business_license']=$data['business_license'];
//                $user_info['legal_person']=$data['legal_person'];
//                $user_info['legal_person_id_front']=$data['legal_person_id_front'];
//                $user_info['legal_person_id_back']=$data['legal_person_id_back'];
//                $user_info['legal_person_id_card']=$data['legal_person_id_card'];
//                $user_info['base_count']=$data['base_count'];
//                $user_info['base_bank_name']=$data['base_bank_name'];
//                $user_info['company_count']=$data['company_count'];
//                $update_data['status'] =1;
                $token = $redis->get('user:' . $user_info['id']);
                $user_info['modify'] = true;
                $redis->hSet('firm_info', $token, json_encode($user_info));
                ajaxReturn('提交成功',1);
            }else{
                ajaxReturn('提交失败');
            }
        }

    }

    /**
     *企业端公司资料上传
     * @author hey_heyang(635870453@qq.com)
     */

    public function companyUpload(){
        if( request()->isPost()){
            $user_info = request()->firm_info;
            $file = request()->file('file');
            $redis = Redis::instance();
            if ($file){
                $dir = 'projectuser'.DS.$user_info['id'];
                $upload_res = companyUpload($file,$dir);
                $upload_res['fileName'] = str_replace('\\','/',$upload_res['fileName']);
                if($upload_res['status']){
                    $user_info['fileName'] = $upload_res['fileName'];
                    $user_info['fileNameSrc'] ='/uploads/projectuser/'.$user_info['id'].'/'.$upload_res['fileName'];
//                    $token = $redis->get('user:' . $user_info['id']);
//                    $user_info['modify'] = true;
//                    $redis->hSet('firm_info', $token, json_encode($user_info));
                    ajaxReturn('上传成功',1,'',$user_info);
                }else{
                    ajaxReturn('上传失败');
                }

            }else{
                ajaxReturn('请选择文件');
            }
        }


    }


    /**
     *前台企业资料下载
     * @author hey_heyang(635870453@qq.com)
     */
    public function downLoad(){
        $param = request()->param();
        $user_info = request()->firm_info;
        QyDataDownLoad($param,$user_info);
    }

    /**
     *公司头像保存
     * @author hey_heyang(635870453@qq.com)
     */
      public function companyAvatar(Request $request){
          if(request()->isPost()){
              $puModel=new ProjectUser();
              $user_info = $request->firm_info;
              $file = request()->file('file');
              if ($file){
                  $dir = 'projectuser'.DS.$user_info['id'];
                  $upload_res = companyUpload($file,$dir);
                  $upload_res['fileName'] = str_replace('\\','/',$upload_res['fileName']);
                  if($upload_res['status']){
                      $user_info['fileName'] = $upload_res['fileName'];
                      $user_info['fileNameSrc'] ='/uploads/projectuser/'.$user_info['id'].'/'.$upload_res['fileName'];
                      $puModel->where(array('id'=>$user_info['id']))->update(array('avatar'=>$upload_res['fileName']));
                      $user_info['avatar'] = $upload_res['fileName'];
                      $redis = Redis::instance();
                      $token = $redis->get('user:' . $user_info['id']);
                      $user_info['modify'] = true;
                      $redis->hSet('firm_info', $token, json_encode($user_info));
                      ajaxReturn('上传成功',1,'',$user_info);
                  }else{
                      ajaxReturn('上传失败');
                  }

              }else{
                  ajaxReturn('请选择文件');
              }
          }
      }
    /**
     *公司头像获取
     * @author hey_heyang(635870453@qq.com)
     */
    public function getCompanyAvatar(Request $request){
        $user_info = $request->firm_info;
        $avatar= self::UPLOAD_SRC.$user_info['id'].'/'.$user_info['avatar'];
        if(!empty($user_info['avatar'])){
            $data['avatar'] = $avatar;
        }else{
            $data['avatar'] = '';
        }

        $result = Config::get('code.success');
        $result['data'] = $data;
        return json($result);
    }


}